package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Dto.DishDto;
import com.itheima.reggie.Dto.SetmealDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoufei
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //1.构造分页对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        //2.构造条件查询对象
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        queryWrapper.like(name !=null,Setmeal::getName,name);
        //执行查询
        setmealService.page(pageInfo,queryWrapper);

        //3创建SetmealDtod的分页对象
        Page<SetmealDto> dtoPage = new Page<>();
        //进行数据拷贝
        List<Setmeal> records = pageInfo.getRecords();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<SetmealDto> list = records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category !=null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);

        return R.success(dtoPage);
    }

    //删除套餐信息
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        log.info("id有{}",ids);
        //删除套餐时，需要删除套餐表中的数据信息，还需要删除关联表中的信息
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 根据Id查询套餐信息
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //1.获取Setmeal表中的分类Id
        Long categoryId = setmeal.getCategoryId();
        //2.创建查询条件查询
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(categoryId!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getCreateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 根据套餐id回显套餐信息
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        //1.根据id查询套餐信息
        Setmeal setmeal = setmealService.getById(id);
        //2.创建套餐扩展对象
        SetmealDto setmealDto = new SetmealDto();
        //3.将套餐信息拷贝到扩展类对象中
        BeanUtils.copyProperties(setmeal,setmealDto);
        //4.根据套餐分类id查询套餐分类
        Long categoryId = setmealDto.getCategoryId();
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,categoryId);
        Category category = categoryService.getById(categoryId);
        //5.根据套餐id作为外键id在套餐菜品表中查询菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper1);
        //6.封装套扩展类对象中
        setmealDto.setCategoryName(category.getName());
        setmealDto.setSetmealDishes(setmealDishList);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());

        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 修改套餐状态
     * @return
     */
    @PostMapping("/status/{data}")
    public R updateSetmealStatus(@PathVariable int data, @RequestParam List<Long> ids){
        //1.根据套餐id修改套餐状态
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId,ids);
        updateWrapper.set(Setmeal::getStatus,data);
        //2.执行方法，返回结果
        setmealService.update(updateWrapper);
        return R.success("修改成功");
    }
}
